Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

add Zone.root to get root zone directly #601

Merged
merged 1 commit into from
Feb 7, 2017

Conversation

JiaLiPassion
Copy link
Collaborator

@JiaLiPassion JiaLiPassion commented Jan 15, 2017

add runOutsideOfCurrentZone method just like runOutsideAngular so we can run under rootZone or fork a new zone under rootZone.

Zone.current.fork({name: 'testZone'}).run(function() {
        Zone.runOutsideOfCurrentZone(() => {
          expect(Zone.current.name).toEqual('<root>');
        });
      });

The motivation is to run some async code inside the zoneSpec's callback. Without the runOutsideOfCurrentZone method, such async call will cause infinite loop.
for example:

Zone.current.fork({
  name: 'testZone', 
  onScheduleTask: function(delegate, currentZone, targetZone, task) {
     asyncLog(task);
     return delegate.scheduleTask(targetZone, task);
  }
});

asyncLog will cause infinite loop, now we can

Zone.current.fork({
  name: 'testZone', 
  onScheduleTask: function(delegate, currentZone, targetZone, task) {
     Zone.runOutsideCurrentZone(() => {
        asyncLog(task);
    });
     return delegate.scheduleTask(targetZone, task);
  }
});

Copy link
Contributor

@mhevery mhevery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, but I don't see the need for this API. I am fine with adding root property, but the rest seems just sugar.

My concern is that new APIs need to be maintained, and documented. Personally my philosophy is to just give the bare minimum and have the developer build on top of that.

@@ -564,6 +571,27 @@ const Zone: ZoneType = (function(global: any) {
}
}

static get rootZone(): AmbientZone {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with this API, but I would rename it to root

return zone;
}

static runOutsideOfCurrentZone(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this API gives too much complexity to the user. Same can be achieved by:

Zone.root.fork(zoneSpec).run(....)

So I don't see the need to have it.

@JiaLiPassion
Copy link
Collaborator Author

JiaLiPassion commented Jan 23, 2017

@mhevery , Ok, got it,I have modified the PR, please review

@JiaLiPassion JiaLiPassion changed the title add runOutsideOfCurrentZone just like runOutsideOfAngular add Zone.root to get root zone directly Jan 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants